package com.jf.hb.ui.interfaces;

import com.jf.hb.ui.JFHandler;
import com.jf.utils.CardSet;

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-13
 * Time:2:13
 */
public class Trick extends Interface {
    private boolean live;

    private final ImageIcon bk;
    private final ImageIcon bk1;
    private final ImageIcon[] trans;
    private int transIndex;

    private CardSet[] cardSet;
    private int cardSetIndex;

    private Stage curStage;

    private ArrayList<String> introduceText;
    private JTextPane introduceJTextPane;
    private SimpleAttributeSet introduceAttributes;
    private int introduceIndex;
    private final Font font;

    private int alpha;

    private JPanel checkPanel;

    private boolean waitSignal;


    enum Stage {
        INTRODUCE,
        SHOW,
        TO_NEXT,
        CHECK,
        TRANSITION
    }

    public Trick() {
        setLayout(null);
        bk = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/trick/bk.png")));
        bk1 = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/trick/bk1.png")));
        setBackground(new Color(32, 56, 100));
        setBackground(Color.BLACK);
        setForeground(Color.BLACK);
        cardSet = new CardSet[6];
        for (int i = 0; i < 6; i++) {
            cardSet[i] = new CardSet(i, true);
            if (i == 0) cardSet[i].setCenter();
        }
        cardSetIndex = 0;
        trans = new ImageIcon[40];
        for (int i = 0; i < 40; i++) {
            trans[i] = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/trick/transition/trans" + i + ".png")));
        }
        transIndex = 0;
        curStage = Stage.INTRODUCE;
        font = new Font("微软雅黑", Font.PLAIN, 32);
        initIntroduce();
        initCheck();
        live = true;
        waitSignal = true;
    }

    private void initIntroduce() {
        introduceText = new ArrayList<>();
        introduceText.add("玩个游戏吧");
        introduceText.add("在心里想一个数字(1-50)\n不要说出声哦~");
        introduceText.add("接下来\n我可以根据你选择的快慢\n隔空猜出你想的数字哟");
        introduceText.add("再次默念你选择的数字^_^");
        introduceIndex = 0;
        alpha = 0;

        introduceJTextPane = new JTextPane();
        introduceJTextPane.setBounds(0, 50, 1280, 300);
        introduceJTextPane.setText(introduceText.getFirst());
        introduceJTextPane.setEditable(false);
        introduceJTextPane.setOpaque(false);
        introduceJTextPane.setFocusable(false);

        introduceAttributes = new SimpleAttributeSet();
        StyleConstants.setFontFamily(introduceAttributes, "华文行楷");
        StyleConstants.setAlignment(introduceAttributes, StyleConstants.ALIGN_CENTER);
        StyleConstants.setFontSize(introduceAttributes, 60);
        StyleConstants.setForeground(introduceAttributes, new Color(255, 162, 31, alpha));

        StyledDocument doc = introduceJTextPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), introduceAttributes, false);
        add(introduceJTextPane);
    }

    private void initCheck() {
        checkPanel = new JPanel();
        checkPanel.setBounds(900, 200, 300, 500);
        checkPanel.setOpaque(false);

        Box box = Box.createVerticalBox();
        Box radioBox = Box.createHorizontalBox();

        JLabel text1 = new JLabel("这组卡牌中有");
        JLabel text2 = new JLabel("你想的数字吗？");
        text1.setFont(font);
        text2.setFont(font);
        text1.setForeground(new Color(255, 162, 31, 170));
        text2.setForeground(new Color(255, 162, 31, 170));

        ButtonGroup bg = new ButtonGroup();
        JRadioButton radioButton1 = new JRadioButton("有");
        radioButton1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        radioButton1.setSelected(false);
        radioButton1.setForeground(Color.WHITE);
        radioButton1.setOpaque(false);
        JRadioButton radioButton2 = new JRadioButton("没有");
        radioButton2.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        radioButton2.setSelected(false);
        radioButton2.setForeground(Color.WHITE);
        radioButton2.setOpaque(false);
        bg.add(radioButton1);
        bg.add(radioButton2);

        JButton checkButton = new JButton(new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/trick/button.png"))));
        checkButton.setOpaque(false);
        checkButton.setContentAreaFilled(false);
        checkButton.setBorderPainted(false);
        checkButton.setFocusPainted(false);
        checkButton.setBorder(null);
        checkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (radioButton1.isSelected() || radioButton2.isSelected()) {
                    CardSet.calculate(radioButton1.isSelected());
                    curStage = Stage.TO_NEXT;
                    checkPanel.setVisible(false);
                    radioButton1.setSelected(false);
                    radioButton2.setSelected(false);
                }
            }
        });

        box.add(text1);
        box.add(text2);
        box.add(Box.createVerticalStrut(20));
        radioBox.add(Box.createHorizontalGlue());
        radioBox.add(radioButton1);
        radioBox.add(Box.createHorizontalStrut(30));
        radioBox.add(radioButton2);
        radioBox.add(Box.createHorizontalGlue());
        box.add(radioBox);
        box.add(Box.createVerticalStrut(20));
        box.add(checkButton);
        checkPanel.add(box);
        add(checkPanel);
        checkPanel.setVisible(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        if (curStage != Stage.TRANSITION) {
            g2d.drawImage(bk.getImage(), 0, 0, getWidth(), getHeight(), null);
            paintCards(g);
        } else {
            g2d.drawImage(bk1.getImage(), 0, 0, bk1.getIconWidth(), bk1.getIconHeight(), null);
            g.drawImage(trans[transIndex/2].getImage(),getWidth()/2 - trans[transIndex/2].getIconWidth() / 2, getHeight()/2 - trans[transIndex/2].getIconHeight() / 2, null);
        }
    }

    private void paintCards(Graphics g) {
        for (CardSet set : cardSet) {
            set.setGraphics(g);
        }
        if (cardSetIndex < cardSet.length) cardSet[cardSetIndex].draw();
        else cardSet[cardSet.length - 1].draw();
    }

    @Override
    public void run() {
        while (live) {
            try {
                switch (curStage) {
                    case INTRODUCE:
                        respIntroduce();
                        break;
                    case SHOW:
                        respShow();
                        break;
                    case TO_NEXT:
                        respToNext();
                        break;
                    case CHECK:
                        respCheck();
                        break;
                    case TRANSITION:
                        if(waitSignal){
                            Thread.sleep(1000);
                            waitSignal = false;
                        }
                        respTransition();
                        break;
                }
                Thread.sleep(75);
                repaint();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void respIntroduce() throws InterruptedException {
        if (alpha >= 170) {
            alpha = 0;
            if (introduceIndex == 0) Thread.sleep(3000);
            if (introduceIndex == 1) Thread.sleep(4800);
            if (introduceIndex == 2) Thread.sleep(5300);
            if (introduceIndex == 3) Thread.sleep(4200);
            introduceIndex++;

            if (introduceIndex > 3) {
                curStage = Stage.SHOW;
                introduceJTextPane.setVisible(false);
                return;
            } else {
                introduceJTextPane.setText(introduceText.get(introduceIndex));
            }
        }
        alpha += 5;
        StyleConstants.setForeground(introduceAttributes, new Color(255, 162, 31, alpha));
        StyledDocument doc = introduceJTextPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), introduceAttributes, false);
    }

    private void respShow() {
        if (cardSet[cardSetIndex].moveToFinal()) {
            if (cardSet[cardSetIndex].flipToFront())
                curStage = Stage.CHECK;
        }
    }

    private void respToNext() throws InterruptedException {
        if (cardSet[cardSetIndex].flipToBack()) {
            if (cardSet[cardSetIndex].moveToInit()) {
                cardSetIndex++;
                if (cardSetIndex >= cardSet.length) {
                    CardSet.endCalculate();
                    curStage = Stage.TRANSITION;
                } else {
                    curStage = Stage.SHOW;
                }
            }
        }
    }

    private void respCheck() {
        checkPanel.setVisible(true);
    }

    private void respTransition() {
        checkPanel.setVisible(false);
        setBackground(Color.BLACK);
        transIndex++;
        if (transIndex / 2 >= trans.length) {
            transIndex--;
            JFHandler.switchSignal = JFHandler.IF.TRICK;
            live = false;
        }
    }
    public void init() {
        remove(checkPanel);
        remove(introduceJTextPane);
        cardSetIndex = 0;
        transIndex = 0;
        curStage = Stage.INTRODUCE;
        initIntroduce();
        initCheck();
        CardSet.initInform();
        cardSet = new CardSet[6];
        for (int i = 0; i < 6; i++) {
            cardSet[i] = new CardSet(i, false);
            if (i == 0) cardSet[i].setCenter();
        }
        cardSetIndex = 0;
        live = true;
        waitSignal = true;
    }
}
